<template>
  <div class="content-main">
    <basicTable ref="resourceListRef" :canResize="true" :schemas="false" :columns="cols" :pagination="true" :exportInfo="false" :request="searchList">
      <template #tableTitle class="table-title">
        <n-button type="primary" @click="addResource"><i class="iconfont icon-banquan_xinzeng"></i>
          新增
        </n-button>
        <n-button @click="deleteResource()" :disabled="!getSelectRowLength"
        ><i class="iconfont icon-banquan_shanchu-03"></i>
          删除
        </n-button>
        <n-button @click="auditResource()" :disabled="!getSelectRowLength"
        ><i class="iconfont icon-banquan_shenhe"></i>
          批量审核
        </n-button>
      </template>
    </basicTable>
  </div>
</template>

<script lang="ts" setup>

import resourceTableConfig from "@/views/resource/share/resource-table";
import {bookList, changeRowStatus, deleteBookRow} from "@/api/resourcelib";
import {showAuditModal} from "@/components/hAudit/audit-service";
let listParams = reactive({});
let resourceListRef = ref(null);
const router = useRouter()
const $dialog = window['$dialog']

const editRow = (e) => {
  console.log(e)
}
const deleteRow = (e) => {
  $dialog.error({
    title: '确定删除该资源？',
    content: '资源删除后，将不可恢复，请谨慎操作',
    positiveText: '确定',
    negativeText: '取消',
    autoFocus:false,
    onPositiveClick: async () => {
      await deleteBookRow([e.id])
      unref(resourceListRef).reload(listParams)
    }
  });
}
const auditRow = (row) => {
  showAuditModal({status: row.status === '未入库' ? 1 : 0}).then(async res => {
    await changeRowStatus({
      ids: [row.id],
      status: res
    })
    unref(resourceListRef).clearCheckRow()
    unref(resourceListRef).reload(listParams)
  });
}
const cols = resourceTableConfig.getBookTableConfig({editRow, deleteRow, auditRow})

const getSelectRowLength = computed(() => {
  return resourceListRef?.value?.getCheckRowKeys().length;
});
const searchList = async (params?) => {
  listParams = params || listParams;
  return await bookList(listParams);
};
const addResource = () => {
router.push('/resource/book-resource-edit')
}
const deleteResource = () => {
  $dialog.error({
    title: '确定删除该资源？',
    content: '资源删除后，将不可恢复，请谨慎操作',
    positiveText: '确定',
    negativeText: '取消',
    autoFocus:false,
    onPositiveClick: async () => {
      await deleteBookRow(resourceListRef?.value?.getCheckRowKeys())
      unref(resourceListRef).clearCheckRow()
      unref(resourceListRef).reload(listParams)
    },
    onNegativeClick: () => {
    }
  });
}

const auditResource = () => {
  showAuditModal({status: 1}).then(async res => {
    await changeRowStatus({
      ids: unref(resourceListRef).getCheckRowKeys(),
      status: res
    })
    unref(resourceListRef).clearCheckRow()
    unref(resourceListRef).reload(listParams)
  });
}

</script>

<style scoped lang="scss">
.table-toolbar-left {
  button {
    margin-right: 10px;
  }
}
</style>
